package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.businessobject.util.PrintPreviewObj2Html;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogVO;
import com.yonyou.pmclouds.repairlog.entity.RepairLogVO;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Component
public class ProblemViewPlugin implements IBillViewCustomPlugin {

    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private StorageFactory storageFactory;
    @Value("${print.perLineNum}")
    private int perLineNum;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {
        userInfoQuery.tranUserInfoField(objects,new String[]{CommonFieldConst.CREATOR,ProblemLogVO.PK_RECTIFICATIONER},new String[]{ProblemLogVO.CREATOR_NAME,ProblemLogVO.RECTIFICATIONER_NAME});
        return objects;
    }

    @Override
    public Map<String, Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {

        ProblemLogVO problemLogVO = ((ProblemLogVO)superVO);

        userInfoQuery.tranUserInfoField(new ProblemLogVO[]{problemLogVO} ,new String[]{CommonFieldConst.CREATOR,ProblemLogVO.PK_RECTIFICATIONER},new String[]{"creator_name",ProblemLogVO.RECTIFICATIONER_NAME});

        EnterpriseInfo enterpriseInfo = enterpriseInfoQuery.queryEnterpriseById(problemLogVO.getPkRectifyEnter());

        problemLogVO.setRectifyEnterName(enterpriseInfo==null?"":enterpriseInfo.getName());

        RepairLogVO[] repairLogVOs =problemLogVO.getRepairlogs();

        PrintPreviewObj2Html obj2Html = new PrintPreviewObj2Html(PrintPreviewObj2Html.BASE_PREFIX, PrintPreviewObj2Html.IMG, perLineNum);

        for(RepairLogVO repairLogVO : repairLogVOs){
            String picHtml = obj2Html.urls2Html(storageFactory.getStorageUtil().signUrlForMultiPictures(repairLogVO.getPictures(), null, request));
            repairLogVO.setStrRepairtime(sdf.format(repairLogVO.getRepair_time()));
            repairLogVO.setPicture_address(picHtml);
        }

        Map<String,Object[]> resultMap = new HashMap<String,Object[]>();

        resultMap.put(BusinessObjectConsts.PM_PROBLEMLOG, new Object[]{problemLogVO});
        if(ArrayUtils.isEmpty(repairLogVOs)){
            RepairLogVO repairLogVO = new RepairLogVO();
            repairLogVO.setPk_problem(problemLogVO.getPk_problem());
            repairLogVO.setPk_problemlog(problemLogVO.getPk_problem());
            repairLogVOs = new RepairLogVO[1];
            repairLogVOs[0] = repairLogVO;
        }
        resultMap.put("pm_repairlog", repairLogVOs);

        return resultMap;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.PROBLEMLOG_BILLTYPE;
    }

}
